package com.py.credit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Account;
import com.py.credit.service.EvalInfoService;
import com.py.credit.utils.DateUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "facade", description = "前台首页")
@RestController
@RequestMapping(value = "/facade")
public class FrontFacadeController {

	@Autowired
	private EvalInfoService evalInfoService;

	@ApiOperation(value = "数字统计数据", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/front/getStatistics.json")
	public RestServiceResult<Object> getStatistics(HttpServletRequest request) {

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		Map<String, Object> stats = new HashMap<String, Object>(4);

		if (account != null) {

			String orgId = account.getOrgId();

			int accountNum = evalInfoService.getAccountNum(orgId);

			int checkedNum = evalInfoService.getPassedCheckNum(orgId);

			int checkingNum = evalInfoService.getProcessCheckNum(orgId);

			List<Map<String, String>> infos = evalInfoService.getEvalInfoByOrgId(orgId);

			// Set<String> set = new HashSet<>();
			// 以申请人为key合并未通过记录数
			Map<String, List<String>> sqrMap = new HashMap<>();

			for (Map<String, String> info : infos) {

				String sqr = info.get("sqr");

				String shzt = info.get("shzt");

				if (sqrMap.containsKey(sqr)) {

					if (!"5".equals(shzt)) {

						sqrMap.get(sqr).add(shzt);

					}

				} else {

					if (!"5".equals(shzt)) {

						List<String> shzts = new ArrayList<String>();

						shzts.add(shzt);

						sqrMap.put(sqr, shzts);
					}

				}

			}

			/*
			 * for (String sqr : sqrMap.keySet()) {
			 * 
			 * List<String> shzts = sqrMap.get(sqr);
			 * 
			 * for (String shzt : shzts) {
			 * 
			 * if (!"5".equals(shzt)) {
			 * 
			 * }
			 * 
			 * }
			 * 
			 * }
			 */
			stats.put("accountNum", accountNum);
			stats.put("checkedAccountNum", accountNum - sqrMap.size());
			stats.put("checkedNum", checkedNum);
			stats.put("checkingNum", checkingNum);

		} else

		{

			ExceptionLogger.error("FrontFacadeController#getStatistics", "未获取到当前用户信息！");

		}

		return new RestServiceResult<Object>(stats);
	}

	@ApiOperation(value = "客户统计数据", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/front/getAccountGraphicStats.json")
	public RestServiceResult<Object> getAccountGraphicStats(HttpServletRequest request,
			@RequestParam(value = "year", required = true, defaultValue = "") String year) {

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		Map<String, Object> stats = new HashMap<String, Object>(2);

		if (account != null) {

			if (StringUtils.isBlank(year)) {

				year = DateUtils.getCurrentYear();

			}

			stats = evalInfoService.getAccountGraphicStats(account.getOrgId(), year);

		} else

		{

			ExceptionLogger.error("FrontFacadeController#getStatistics", "未获取到当前用户信息！");

		}

		return new RestServiceResult<Object>(stats);
	}

	@ApiOperation(value = "审核统计", httpMethod = "GET", produces = "application/json")
	@RequestMapping(value = "/front/getCheckGraphicStats.json")
	public RestServiceResult<Object> getCheckGraphicStats(HttpServletRequest request,
			@RequestParam(value = "year", required = true, defaultValue = "") String year) {

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		Map<String, Object> stats = new HashMap<>(2);

		if (account != null) {

			if (StringUtils.isBlank(year)) {

				year = DateUtils.getCurrentYear();

			}

			stats = evalInfoService.getCheckGraphicStats(account.getOrgId(), year);

		} else

		{

			ExceptionLogger.error("FrontFacadeController#getCheckGraphicStats", "未获取到当前用户信息！");

		}

		return new RestServiceResult<Object>(stats);
	}
}
